from models import SimilarityMatrix, Sequence
from calc import SmithWaterman

#operacje na SimilarityMatrix	
def addSimilarityMatrix():
	matrix = SimilarityMatrix()
	matrix.save()
	return matrix.id
	
def delSimilarityMatrix(matrix_id):
	matrix = SimilarityMatrix.objects.get(id = matrix_id)
	matrix.delete()
	return
	
def readSimilarityMatrixNumber():
	matrix = SimilarityMatrix.objects.all()
	return len(matrix)

def checkSimilarityMatrix(matrix_id):
	try:
		matrix = SimilarityMatrix.objects.get(id = matrix_id)
		return 1
	except:
		return -1

def readSimilarityMatrixIDs():
	ids = []
	for o in SimilarityMatrix.objects.all():
		ids.append(o.id)

	return ids

def readSimilarityMatrixDetails(matrix_id):
	try:
		matrix = SimilarityMatrix.objects.get(id = matrix_id)
		return matrix.getMatrixElementsDescr()
	except:
		return -1

#operacje na MatrixElement
def addMatrixElement(matrix_id, el):
	matrix = SimilarityMatrix.objects.get(id = matrix_id)
	return matrix.addMatrixElements(el)

def modMatrixElement(matrix_id, element_id, new_el):
	matrix = SimilarityMatrix.objects.get(id = matrix_id)
	matrix.modMatrixElement(element_id, new_el)
	return

#operacje na Sequence
def addSequence(n, s):
	seq = Sequence(name = n, sequence = s)
	seq.save()
	return seq.id

def delSequence(seq_id):
	seq = Sequence.objects.get(id = seq_id)
	seq.delete()
	return

def readSequences():
	l = []
	for o in Sequence.objects.all():
		l.append( {"name" : o.name, "id" : o.id, "sequence" : o.sequence} )
	return l

def readSequenceDetails(seq_id):
       	seq = Sequence.objects.get(id = seq_id)
       	return seq.getDetails()

def startCalculation(sm_id, seq1_id, seq2_id, end_value):
	matrix = SimilarityMatrix.objects.get(id = sm_id)
	seq1 = Sequence.objects.get(id = seq1_id)
	seq2 = Sequence.objects.get(id = seq2_id)

	print end_value

	sw = SmithWaterman.getInstance()
	sw.startCalculation(matrix, seq1.sequence.__str__(), seq2.sequence.__str__(), int(end_value))
	return

def getFirstResult():
	sw = SmithWaterman.getInstance()
	return sw.getFirstResult()

def getSecondResult():
	sw = SmithWaterman.getInstance()
	return sw.getSecondResult()
